import { $Platform, $AuthorizeStatus } from "@/utils/const/env";
import { siteinfo } from "@/siteinfo";

export const info = () => uni.getSystemInfoSync();
export const isIos = () => {
	return info().osName === 'ios';
};
export const isAndroid = () => {
	return info().osName === 'android';
};
export const isWechat = () => {
	return false;
};
export const platforms = (): string[] => {
	let result: string[] = [];
	if (isIos()) {
		result.push($Platform.APP_IOS);
	} else if (isAndroid()) {
		result.push($Platform.APP_ANDROID);
	}
	return result;
};
export const isDev = () => process.env.NODE_ENV !== 'production';
export const isProd = () => process.env.NODE_ENV === 'production';
export const appid = () => siteinfo.app_id;
export const host = () => siteinfo.siteroot;
export const name = () => siteinfo.name;
export const hasAndroidPermission = (permission: string | string[]) => {
	return new Promise((resolve, reject) => {
		if (isAndroid()) {
			plus.android.requestPermissions(permission,
				(res: any) => {
					if ((res.granted.length > 0 && res.granted.every((item: any) => permission.includes(item))) || (res.deniedPresent.length > 0 && res.deniedPresent.every((item: any) => permission.includes(item)))) {
						resolve(true);
					} else {
						reject(1);
					}
				},
				(err:any) => {
					reject(1);
				}
			);
		} else {
			reject(1);
		}
	})
}
export const chooseImageFromCamera = (options: UniNamespace.ChooseImageOptions) => {
	const appAuthorizeSetting = uni.getAppAuthorizeSetting();
	if (appAuthorizeSetting) {
		switch (appAuthorizeSetting.cameraAuthorized) {
			case $AuthorizeStatus.DENIED:
				uni.chooseImage({
					count: 1,
					sizeType: ['original'],
					...options,
					sourceType: ['camera'],
					fail: () => {
						uni.showModal({
							title: '提示',
							content: '请先开启“相机权限”',
							success: (res: any) => {
								if (res.confirm) {
									uni.openAppAuthorizeSetting({
										success: () => { }
									})
								}
							}
						})
					}
				});
				break;
			case $AuthorizeStatus.AUTHORIZED:
			case $AuthorizeStatus.NOT_DETERMINED:
				uni.chooseImage({
					count: 1,
					sizeType: ['original'],
					...options,
					sourceType: ['camera'],
				});
				break;
			case $AuthorizeStatus.CONFIG_ERROR:
				break;
		}
	}

}
const albumPermission = ['android.permission.READ_EXTERNAL_STORAGE', 'android.permission.WRITE_EXTERNAL_STORAGE'];
export const chooseImageFromAlbum = (options: UniNamespace.ChooseImageOptions) => {
	if (isIos()) {
		const appAuthorizeSetting = uni.getAppAuthorizeSetting();
		if (appAuthorizeSetting) {
			switch (appAuthorizeSetting.albumAuthorized) {
				case $AuthorizeStatus.DENIED:
					uni.showModal({
						title: '提示',
						content: '请先开启“相册权限”',
						success: (res: any) => {
							if (res.confirm) {
								uni.openAppAuthorizeSetting({
									success: () => { }
								})
							}
						}
					})
					break;
				case $AuthorizeStatus.AUTHORIZED:
				case $AuthorizeStatus.NOT_DETERMINED:
					uni.chooseImage({
						count: 1,
						sizeType: ['original'],
						...options,
						sourceType: ['album'],
					});
					break;
				case $AuthorizeStatus.CONFIG_ERROR:
					break;
			}
		}
	} else {
		hasAndroidPermission(albumPermission).then(() => {
			uni.chooseImage({
				count: 1,
				sizeType: ['original'],
				...options,
				sourceType: ['album'],
			});
		}).then((err: any) => {
			if (err === 1) {
				uni.showModal({
					title: '提示',
					content: '请先开启“相册权限”或“媒体和文件权限”',
					success: (res: any) => {
						if (res.confirm) {
							uni.openAppAuthorizeSetting({
								success: () => { }
							})
						}
					}
				})
			}
		})
	}
}
export const chooseImage = (options: UniNamespace.ChooseImageOptions) => {
	if (options.sourceType && options.sourceType.length === 1) {
		if (options.sourceType.includes('camera')) {
			chooseImageFromCamera(options)
		} else if (options.sourceType.includes('album')) {
			chooseImageFromAlbum(options)
		}
	} else {
		uni.showActionSheet({
			title: '选择图片来源',
			itemList: ['拍照', '从手机相册选择'],
			success: (res: any) => {
				if (!res.cancel) {
					if (res.tapIndex === 0) {
						chooseImageFromCamera(options)
					} else if (res.tapIndex === 1) {
						chooseImageFromAlbum(options)
					}
				}
			}
		});
	}
}
export const chooseVideoFromCamera = (options: UniNamespace.ChooseVideoOptions) => {
	const appAuthorizeSetting = uni.getAppAuthorizeSetting();
	if (appAuthorizeSetting) {
		switch (appAuthorizeSetting.cameraAuthorized) {
			case $AuthorizeStatus.DENIED:
				uni.chooseVideo({
					...options,
					sourceType: ['camera'],
					fail: () => {
						uni.showModal({
							title: '提示',
							content: '请先开启“相机权限”',
							success: (res: any) => {
								if (res.confirm) {
									uni.openAppAuthorizeSetting({
										success: () => { }
									})
								}
							}
						})
					}
				});
				break;
			case $AuthorizeStatus.AUTHORIZED:
			case $AuthorizeStatus.NOT_DETERMINED:
				uni.chooseVideo({
					...options,
					sourceType: ['camera'],
				});
				break;
			case $AuthorizeStatus.CONFIG_ERROR:
				break;
		}
	}

}
export const chooseVideoFromAlbum = (options: UniNamespace.ChooseVideoOptions) => {
	if (isIos()) {
		const appAuthorizeSetting = uni.getAppAuthorizeSetting();
		if (appAuthorizeSetting) {
			switch (appAuthorizeSetting.albumAuthorized) {
				case $AuthorizeStatus.DENIED:
					uni.showModal({
						title: '提示',
						content: '请先开启“相册权限”',
						success: (res: any) => {
							if (res.confirm) {
								uni.openAppAuthorizeSetting({
									success: () => { }
								})
							}
						}
					})
					break;
				case $AuthorizeStatus.AUTHORIZED:
				case $AuthorizeStatus.NOT_DETERMINED:
					uni.chooseVideo({
						...options,
						sourceType: ['album'],
					});
					break;
				case $AuthorizeStatus.CONFIG_ERROR:
					break;
			}
		}
	} else {
		hasAndroidPermission(albumPermission).then(() => {
			uni.chooseVideo({
				...options,
				sourceType: ['album'],
			});
		}).then((err: any) => {
			if (err === 1) {
				uni.showModal({
					title: '提示',
					content: '请先开启“相册权限”或“媒体和文件权限”',
					success: (res: any) => {
						if (res.confirm) {
							uni.openAppAuthorizeSetting({
								success: () => { }
							})
						}
					}
				})
			}
		})
	}
}
export const chooseVideo = (options: UniNamespace.ChooseVideoOptions) => {
	if (options.sourceType && options.sourceType.length === 1) {
		if (options.sourceType.includes('camera')) {
			chooseVideoFromCamera(options)
		} else if (options.sourceType.includes('album')) {
			chooseVideoFromAlbum(options)
		}
	} else {
		uni.showActionSheet({
			title: '选择视频来源',
			itemList: ['拍照', '从手机相册选择'],
			success: (res: any) => {
				if (!res.cancel) {
					if (res.tapIndex === 0) {
						chooseVideoFromCamera(options)
					} else if (res.tapIndex === 1) {
						chooseVideoFromAlbum(options)
					}
				}
			}
		});
	}
}
export const saveImageToPhotosAlbum = (options: UniNamespace.SaveImageToPhotosAlbumOptions) => {
	if (isIos()) {
		const appAuthorizeSetting = uni.getAppAuthorizeSetting();
		if (appAuthorizeSetting) {
			switch (appAuthorizeSetting.albumAuthorized) {
				case $AuthorizeStatus.DENIED:
					uni.showModal({
						title: '提示',
						content: '请先开启“相册权限”',
						success: (res: any) => {
							if (res.confirm) {
								uni.openAppAuthorizeSetting({
									success: () => { }
								})
							}
						}
					})
					break;
				case $AuthorizeStatus.AUTHORIZED:
				case $AuthorizeStatus.NOT_DETERMINED:
					uni.saveImageToPhotosAlbum({
						...options
					});
					break;
				case $AuthorizeStatus.CONFIG_ERROR:
					break;
			}
		}
	} else {
		hasAndroidPermission(albumPermission).then(() => {
			uni.saveImageToPhotosAlbum({
				...options
			});
		}).then((err: any) => {
			if (err === 1) {
				uni.showModal({
					title: '提示',
					content: '请先开启“相册权限”或“媒体和文件权限”',
					success: (res: any) => {
						if (res.confirm) {
							uni.openAppAuthorizeSetting({
								success: () => { }
							})
						}
					}
				})
			}
		})
	}
}
export const scanCode = (options?: UniNamespace.ScanCodeOptions) => {
	const appAuthorizeSetting = uni.getAppAuthorizeSetting();
	if (appAuthorizeSetting) {
		switch (appAuthorizeSetting.cameraAuthorized) {
			case $AuthorizeStatus.DENIED:
				uni.scanCode({
					onlyFromCamera: true,
					...options,
					fail: () => {
						uni.showModal({
							title: '提示',
							content: '请先开启“相机权限”',
							success: (res: any) => {
								if (res.confirm) {
									uni.openAppAuthorizeSetting({
										success: () => { }
									})
								}
							}
						})
					}
				});
				break;
			case $AuthorizeStatus.AUTHORIZED:
			case $AuthorizeStatus.NOT_DETERMINED:
				uni.scanCode({
					onlyFromCamera: true,
					...options
				});
				break;
			case $AuthorizeStatus.CONFIG_ERROR:
				break;
		}
	}
}